# -*- coding: utf-8 -*-

#   This file is part of pynix.

# Copyright (c) 2009 - Fabiano Francesconi, Walter Da Col

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

''' utility module '''

import os

# numerate colors
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)

# color sequence
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[0;%dm"
BOLD_SEQ = "\033[1m"

# global variable to manage colored output
use_color = True

def is_running_as_root():
    ''' returns true if pynix is running as root user '''
    if (os.getenv("USER") == "root"):
        return True
    return False

def set_use_color(value):
    ''' set utils global variable use_color for colored output '''
    global use_color
    use_color = value

def get_file_exists(filename):
    return (os.path.isfile(filename))

def black(msg):
    ''' output msg with black color '''
    if (use_color):
        msg = COLOR_SEQ % (BLACK) + msg + RESET_SEQ
    return msg

def green(msg):
    ''' output msg with darkgreen color '''
    if (use_color):
        msg = COLOR_SEQ % (GREEN) + msg + RESET_SEQ
    return msg

def red(msg):
    ''' output msg with red color '''
    if (use_color):
        msg = COLOR_SEQ % (RED) + msg + RESET_SEQ
    return msg

def blue(msg):
    ''' output msg with blue color '''
    if (use_color):
        msg = COLOR_SEQ % (BLUE) + msg + RESET_SEQ
    return msg

def yellow(msg):
    ''' output msg with yellow color '''
    if (use_color):
        msg = COLOR_SEQ % (YELLOW) + msg + RESET_SEQ
    return msg

def magenta(msg):
    ''' output msg magenta colored '''
    if (use_color):
        msg = COLOR_SEQ % (MAGENTA) + msg + RESET_SEQ
    return msg

def cyan(msg):
    ''' output msg with cyan color '''
    if (use_color):
        msg = COLOR_SEQ % (CYAN) + msg + RESET_SEQ
    return msg

def white(msg):
    ''' output msg whitely colored '''
    if (use_color):
        msg = COLOR_SEQ % (WHITE) + msg + RESET_SEQ
    return msg
